package com.tanhua.dubbo.api;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tan.model.mongo.Video;
import com.tan.model.vo.PageResult;
import com.tanhua.dubbo.utils.IdWorker;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@DubboService
public class VideoApiimpl implements VideoApi{

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    IdWorker idWorker;


    @Override
    public void saveVideo(String imageUrl, String videoUrl, Long userid) {
        Video video=new Video();
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        video.setUserId(userid);
        video.setPicUrl(imageUrl);
        video.setVideoUrl(videoUrl);
        video.setText("烟火");
        mongoTemplate.save(video);

    }

    @Override
    public List<Video> queryVideoList(List<Long> vids) {
        if(vids!=null&&vids.size()>0){
            Criteria criteria=Criteria.where("vid").in(vids);
            Query query=new Query(criteria).with(Sort.by(Sort.Direction.DESC,"created"));
            List<Video> videoList = mongoTemplate.find(query, Video.class);
            return videoList;
        }
       return new ArrayList<>();
    }

    @Override
    public List<Video> queryVideoListnotvid(Integer page, Integer pagesize) {
        Criteria criteria=Criteria.where("vid").ne(null);
        Query query=new Query(criteria).with(Sort.by(Sort.Direction.DESC,"created"))
                .limit(pagesize).skip((page-1)*pagesize);
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        return videoList;
    }

    @Override
    public void likeVideo(String id) {
        Criteria criteria=Criteria.where("id").is(id);
        Query query=new Query(criteria);
        Update update=new Update();
        update.inc("likeCount",1);
        mongoTemplate.updateFirst(query,update,Video.class);
    }

    @Override
    public void dislikeVideo(String  id) {
        Criteria criteria=Criteria.where("id").is(id);
        Query query=new Query(criteria);
        Update update=new Update();
        update.inc("likeCount",-1);
        mongoTemplate.updateFirst(query,update,Video.class);
    }

    @Override
    public PageResult findByUserId(Integer page, Integer pagesize, Long uid) {
        Criteria criteria=Criteria.where("userId").is(uid);
        Query query=new Query(criteria).with(Sort.by(Sort.Direction.DESC,"created"))
                .limit(pagesize).skip((page-1)*pagesize);
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        if (videoList!=null){
            PageResult pageResult = new PageResult(page,pagesize, 0,videoList);
            return pageResult;
        }
        return new PageResult();
    }


}
